package nettyProtocol.test;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import nettyProtocol.pojo.Header;
import nettyProtocol.pojo.NettyMessage;
import nettyProtocol.serverAndClient.NettyServer;

public class Server  extends NettyServer {
    public static void main(String[] args) throws Exception {
        new Server().bind();
    }

    @Override
    public void initChannel(ChannelPipeline pipeline) {
        pipeline.addLast(new MessageRespHandler());
    }

    private class MessageRespHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println(msg);
            System.out.println("收到客户端传来的消息,服务端进行处理,返回处理信息给客户端");
            NettyMessage message = new NettyMessage();
            Header header = new Header();
            header.setType((byte)1);
            header.setSessionID(100L);
            message.setHeader(header);
            message.setBody("服务端处理的消息");
            ctx.writeAndFlush(message);
        }
    }
}
